package net.adetech.crj.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.dao.ApplyMapper;
import net.adetech.crj.dao.AppointmentMapper;
import net.adetech.crj.dao.RemindMapper;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.Remind;
import net.adetech.crj.entity.User;
import net.adetech.crj.entity.tb.*;
import net.adetech.crj.utils.ApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service("zggmtb.submit")
public class ZggmtbSubmitService implements BaseService {

    @Autowired
    ApiUtils apiUtils;
    @Autowired
    ApplyMapper applyMapper;

    @Autowired
    AppointmentMapper appointmentMapper;

    @Autowired
    RemindMapper remindMapper;


    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        String sqh = null;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ZggmtbForm form = (ZggmtbForm) params.get("form");
        User user = (User) params.get("user");
        if (user==null)
        {
            user = new User();
            user.setIdNumber("null");
        }

        ZggmtbResult result = new ZggmtbResult();

        //办理普通护照
        if (form.getSqlb().contains("101")) {
            String ywbh = apiUtils.getYwbh();
            if (sqh == null) {
                sqh = ywbh;
            }
            result.setHzYwbh(ywbh);
            String args = this.getHzArgs(ywbh, sqh, form);
            System.out.println(args);
            resultMap.put("args_hz", args);
            JSONObject response = apiUtils.call("sq_savePreApply", args);
            resultMap.put("response_hz", response);


        }

        //办理港澳通行证
        if (form.getSqlb().contains("102") || form.getQwd().contains("HKG") || form.getQwd().contains("MAC")) {
            String ywbh = apiUtils.getYwbh();
            if (sqh == null) {
                sqh = ywbh;
            }
            result.setGaYwbh(ywbh);

            String args = getGaArgs(ywbh, sqh, form);
            System.out.println(args);
            resultMap.put("args_ga", args);
            JSONObject response = apiUtils.call("sq_savePreApply", args);
            resultMap.put("response_ga", response);


        }

        //办理台湾通行证
        if (form.getSqlb().contains("104") || form.getQwd().contains("TWN")) {
            String ywbh = apiUtils.getYwbh();
            if (sqh == null) {
                sqh = ywbh;
            }
            result.setTwYwbh(ywbh);
            String args = getTwArgs(ywbh, sqh, form);
            System.out.println(args);
            resultMap.put("args_tw", args);

            JSONObject response = apiUtils.call("sq_savePreApply", args);
            resultMap.put("response_tw", response);

        }
        result.setSqh(sqh);


        //JSONArray tipsArray = new JSONArray();

        String sfzh = form.getSfzh();
        boolean officialFlag = false;
        int age = this.caluAge(sfzh);
        boolean yantaiFlag = checkYantai(form);
        boolean shandongFlag = checkShandong(form);
        boolean otherCityFlag = checkOtherCity(form);

        JSONObject officialResult = apiUtils.queryBySfzh("kz_queryOfficial", sfzh);
        if (officialResult.getInteger("success") == 1) {
            officialFlag = true;
        }

        //办理护照
        if(form.getSqlb().contains("101"))
        {
            //省内国家工作人员
            if (officialFlag&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0101"));

            }
            //省内满16周岁
            else if(age>=16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0102"));

            }

            //省内未满16周岁
            else if (age<16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0103"));

            }
            //省外满16周岁
            else if(age>=16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0111"));

            }
            //省外未满16周岁
            else if (age<16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0112"));

            }

        }


        boolean gaqzRemindFlag = false;



        //前往港澳
        if (form.getQwd().contains("HKG") || form.getQwd().contains("MAC")) {
            //团队旅游
            if ((form.getQwd().contains("HKG")&&"12".equals(form.getQzzlHkg()) )|| (form.getQwd().contains("MAC")&&"12".equals(form.getQzzlMac()))) {

                //省内户籍未满16周岁
                if (age < 16 && shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0211"));
                    gaqzRemindFlag =true;
                }
                //省外户籍未满16周岁
                else if (age < 16 && !shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0212"));
                    gaqzRemindFlag =true;

                }
                //省外户籍,非国家工作人员
                else if (!officialFlag && !shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0201"));
                    gaqzRemindFlag =true;
                }
                //省内户籍,国家工作人员
                else if (officialFlag && shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0221"));
                    gaqzRemindFlag =true;
                }

                //省内户籍,非国家工作人员,年满16周岁
                else if (shandongFlag&&!officialFlag&&age>=16)
                {
                    result.addRemind(remindMapper.getRemindByCode("T0213"));
                    gaqzRemindFlag =true;
                }
            }


            //商务签注
            if ("13".equals(form.getQzzlHkg()) || "13".equals(form.getQzzlMac())) {
                //申请一次港澳商务签注
                if (("13".equals(form.getQzzlHkg()) && form.getQzcsHkg().substring(2).equals("01"))||("13".equals(form.getQzzlMac()) && form.getQzcsMac().substring(2).equals("01")))
                {
                    result.addRemind(remindMapper.getRemindByCode("T0231"));
                    gaqzRemindFlag =true;
                }
                //申请一次港澳商务签注
                if (("13".equals(form.getQzzlHkg()) && !form.getQzcsHkg().substring(2).equals("01"))||("13".equals(form.getQzzlMac()) && !form.getQzcsMac().substring(2).equals("01")))
                {
                    result.addRemind(remindMapper.getRemindByCode("T0232"));
                    gaqzRemindFlag =true;
                }
            }
        }

        //办理港澳通行证
        if (!gaqzRemindFlag&&form.getSqlb().contains("102"))
        {
            //省内国家工作人员
            if (officialFlag&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0281"));

            }
            //省内满16周岁
            else if(age>=16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0282"));

            }

            //省内未满16周岁
            else if (age<16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0283"));

            }
            //省外满16周岁
            else if(age>=16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0284"));

            }
            //省外未满16周岁
            else if (age<16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0285"));

            }
        }




        boolean twqzRemindFlag = false;





        //前往台湾
        if (form.getQwd().contains("TWN")) {
            //个人旅游签注
            if (form.getQzzlTwn().equals("2B")) {
                //烟台户籍未满16周岁
                if (age < 16 && checkYantai(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0311"));
                    twqzRemindFlag =true;
                }
                //省内非烟台户籍未满16周岁
                else if (age < 16 && checkOtherCity(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0312"));
                    twqzRemindFlag =true;
                }
                //省外户籍未满16周岁
                else if (age < 16 && !checkShandong(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0313"));
                    twqzRemindFlag =true;
                }
                //烟台户籍,非国家工作人员,年满16周岁
                else if (!officialFlag && yantaiFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0303"));
                    twqzRemindFlag =true;
                }
                //省内非烟台户籍,非国家工作人员
                else if (!officialFlag && checkOtherCity(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0301"));
                    twqzRemindFlag =true;
                }
                //省外户籍,非国家工作人员
                else if (!officialFlag && !checkShandong(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0302"));
                    twqzRemindFlag =true;
                }

                //烟台户籍,国家工作人员
                else if (officialFlag && yantaiFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0322"));
                    twqzRemindFlag =true;
                }
                //省内非烟台户籍,国家工作人员
                else if (officialFlag && otherCityFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0321"));
                    twqzRemindFlag =true;
                }

            }
            //台湾团队旅游
            else if (form.getQzzlTwn().equals("25")) {

                //省内户籍未满16周岁
                if (age < 16 && shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0341"));
                    twqzRemindFlag =true;
                }
                //省外户籍未满16周岁
                else if (age < 16 && !checkShandong(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0342"));
                    twqzRemindFlag =true;
                }//省外户籍,非国家工作人员
                else if (!officialFlag && !checkShandong(form)) {
                    result.addRemind(remindMapper.getRemindByCode("T0331"));
                    twqzRemindFlag =true;
                }
                //省内户籍,国家工作人员
                else if (officialFlag && shandongFlag) {
                    result.addRemind(remindMapper.getRemindByCode("T0351"));
                    twqzRemindFlag =true;
                }
                //省内户籍,非国家工作人员,年满16周岁
                else if (!officialFlag && shandongFlag&&age>=16) {
                    result.addRemind(remindMapper.getRemindByCode("T0352"));
                    twqzRemindFlag =true;
                }
            }
        }

        //办理台湾通行证
        if (!twqzRemindFlag&&form.getSqlb().contains("104"))
        {
            //省内国家工作人员
            if (officialFlag&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0381"));

            }
            //省内满16周岁
            else if(age>=16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0382"));

            }

            //省内未满16周岁
            else if (age<16&&shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0383"));

            }
            //省外满16周岁
            else if(age>=16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0384"));

            }
            //省外未满16周岁
            else if (age<16&&!shandongFlag)
            {
                result.addRemind(remindMapper.getRemindByCode("T0385"));

            }
        }

//        //查询是否国家工作人员
//
//
//
//        //判断是否未满16岁
//        int age = this.caluAge(sfzh);
//        if (age<16)
//        {
//            JSONObject tips = new JSONObject();
//            tips.put("reason","age");
//            tips.put("content",age);
//            tipsArray.add(tips);
//        }
//        //判断是否商务备案
//        if("13".equals(form.getQzzlHkg())||"13".equals(form.getQzzlMac())||"27".equals(form.getQzzlTwn()))
//        {
//            JSONObject businessResult = apiUtils.queryBySfzh("jg_queryBadwBasic",sfzh);
//            JSONObject tips = new JSONObject();
//            tips.put("reason","business");
//            tips.put("content",businessResult);
//            tipsArray.add(tips);
//        }

        String remindJson = JSON.toJSONString(result.getTipsList());


        List<Apply> originalApplyList = applyMapper.getApplyListByIdNumberAndApplyUserId(form.getSfzh(), user.getIdNumber());

        //原预约id
        Long originalAppointmentId = null;
        if (originalApplyList!=null&& originalApplyList.size()>0)
        {
            originalAppointmentId = originalApplyList.get(0).getAppointmentId();
        }



        applyMapper.updateApplyStateByIdNumberAndApplyUserId(form.getSfzh(), user.getIdNumber(), "0");


        Apply apply = new Apply();
        apply.setApplyUserId(user.getIdNumber());
        apply.setRealName(form.getZwx() + form.getZwm());
        apply.setIdNumber(form.getSfzh());
        apply.setIdType("1");
        apply.setApplySummary("A");
        apply.setApplyDetail(JSON.toJSONString(form));
        apply.setApplyNo(sqh);
        apply.setPhoneNumber(form.getLxdh());
        apply.setRemindJson(remindJson);

        applyMapper.saveApply(apply);
        apply = applyMapper.getApplyByYwbh(sqh);

        if(originalAppointmentId!=null&&originalAppointmentId!=0)
        {
            appointmentMapper.updateApplyId(originalAppointmentId,apply.getApplyId());
        }


        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            apply.setRealName(apply.getRealName() + "女士");
        } else {
            apply.setRealName(apply.getRealName() + "先生");
        }






        resultMap.put("result", result);
        //resultMap.put("tipsArray", tipsArray);

        resultMap.put("apply", apply);


        return resultMap;
    }


    /**
     * 检查省内非烟台户籍人员
     *
     * @param form
     * @return
     */
    private boolean checkOtherCity(ZggmtbForm form) {
        if (form.getHkszd().indexOf("37") == 0) {
            if (form.getHkszd().indexOf("3706") != 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查省内户籍人员
     *
     * @param form
     * @return
     */
    private boolean checkShandong(ZggmtbForm form) {
        if (form.getHkszd().indexOf("37") == 0) {

            return true;

        }
        return false;
    }

    /**
     * 检查烟台本地户籍
     *
     * @param form
     * @return
     */
    private boolean checkYantai(ZggmtbForm form) {
        if (form.getHkszd().indexOf("3706") == 0) {
            return true;
        }
        return false;
    }


    /**
     * 生成港澳通行证调用代码
     *
     * @param ywbh 业务编号
     * @param sqh
     * @param form
     * @return
     */
    private String getGaArgs(String ywbh, String sqh, ZggmtbForm form) {
        String sfzh = form.getSfzh();
        String xb = "1";
        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            xb = "2";
        }
        String csrq = sfzh.substring(6, 14);

        SqSavePreApplyArgs args = new SqSavePreApplyArgs();

        args.setSqlb("102"); //往来港澳通行证
        SqxxInfo sqxxInfo = new SqxxInfo();
        args.setInfo(sqxxInfo);
        TbjNdjm cjsqxx = new TbjNdjm();
        sqxxInfo.setCjsqxx(cjsqxx);
        cjsqxx.setYwbh(ywbh);
        cjsqxx.setSqh(sqh);
        cjsqxx.setYwm(form.getYwm());
        cjsqxx.setYwx(form.getYwx());
        cjsqxx.setYwxm(form.getYwx() + " " + form.getYwm());
        cjsqxx.setZwx(form.getZwx());
        cjsqxx.setZwm(form.getZwm());
        cjsqxx.setZwxm(form.getZwx() + form.getZwm());

        cjsqxx.setLxdh(form.getLxdh());
        cjsqxx.setSjhm(form.getSjhm());
        cjsqxx.setXb(xb);  //性别:男
        cjsqxx.setCsrq(csrq);
        cjsqxx.setSfzh(sfzh);
        cjsqxx.setGjdq("CHN"); //国家地区:中国
        cjsqxx.setRydylb("R"); //人员地域类型:内地居民
        cjsqxx.setSqlb("102"); //申请类别：往来港澳通行证
        if (form.getSqlb().contains("102")) {
            cjsqxx.setBzlb(form.getBzlbGa());  //办证类型
        } else {
            cjsqxx.setBzlb("81");   //办证类型：单独签注
        }


        cjsqxx.setCsd(form.getCsd()); //出生地:山东

        cjsqxx.setMz(form.getMz());  //民族:汉
        cjsqxx.setHkszd(form.getHkszd()); //山东省烟台市芝罘区
        cjsqxx.setJjqklxr(form.getJjqklxr());
        cjsqxx.setJjqklxrdh(form.getJjqklxrdh());

        //现持证件信息
        if (!"".equals(form.getXczjhmGa())) {
            cjsqxx.setXczjzl("21"); //港澳通行证
            cjsqxx.setXczjhm(form.getXczjhmGa());
            cjsqxx.setXczjyzqz(form.getXczjyxqzGa());
        }


        //取证信息
        cjsqxx.setSfxtkzd(form.getSfxtkzd());
        if ("1".equals(form.getSfxtkzd())) {
            cjsqxx.setEmsdz(form.getEmsdz());
            cjsqxx.setYzbm(form.getYzbm());
            cjsqxx.setSjr(form.getSjr());
            cjsqxx.setLxdh(form.getLxdh());
            cjsqxx.setSjhm(form.getSjhm());
        }


        if (form.getQwd().contains("HKG") || form.getQwd().contains("MAC")) {
            sqxxInfo.setQzblxx(new ArrayList<TbjQzblxx>());
            int xh = 1;
            if (form.getQwd().contains("HKG")) {
                String qzcs = form.getQzcsHkg();
                String qzyxq = null;
                String qzyxqdw = null;
                String qzyxcs = qzcs.substring(3, 4);
                if (qzcs.substring(0, 2).equals("03")) {
                    qzyxq = "3";
                    qzyxqdw = "月";
                } else if (qzcs.substring(0, 2).equals("06")) {
                    qzyxq = "06";
                    qzyxqdw = "月";
                } else if (qzcs.substring(0, 2).equals("12")) {
                    qzyxq = "1";
                    qzyxqdw = "年";
                } else if (qzcs.substring(0, 2).equals("99")) {
                    qzyxq = "3";
                    qzyxqdw = "年";
                }

                TbjQzblxx qzblxx = new TbjQzblxx();
                qzblxx.setYwbh(ywbh);
                qzblxx.setXh(xh + "");
                xh++;
                qzblxx.setQwd("HKG"); //前往地:香港
                qzblxx.setZjzl("21"); //证件类型：港澳通行证
                qzblxx.setQzzl(form.getQzzlHkg()); //签注类型：港澳团队旅游
                qzblxx.setQzyxq(qzyxq); //有效期
                qzblxx.setQzyxqdw(qzyxqdw); //有效期单位
                qzblxx.setQzyxcs(qzyxcs); //有效次数
                sqxxInfo.getQzblxx().add(qzblxx);
            }
            if (form.getQwd().contains("MAC")) {
                String qzcs = form.getQzcsMac();
                String qzyxq = null;
                String qzyxqdw = null;
                String qzyxcs = qzcs.substring(3, 4);
                if (qzcs.substring(0, 2).equals("03")) {
                    qzyxq = "3";
                    qzyxqdw = "月";
                } else if (qzcs.substring(0, 2).equals("99")) {
                    qzyxq = "1";
                    qzyxqdw = "年";
                } else if (qzcs.substring(0, 2).equals("12")) {
                    qzyxq = "1";
                    qzyxqdw = "年";
                }


                TbjQzblxx qzblxx = new TbjQzblxx();
                qzblxx.setYwbh(ywbh);
                qzblxx.setXh(xh + "");
                qzblxx.setQwd("MAC"); //前往地:澳门
                qzblxx.setZjzl("21"); //证件类型：港澳通行证
                qzblxx.setQzzl(form.getQzzlMac()); //签注类型：港澳团队旅游
                qzblxx.setQzyxq(qzyxq); //有效期
                qzblxx.setQzyxqdw(qzyxqdw); //有效期单位
                qzblxx.setQzyxcs(qzyxcs); //有效次数
                sqxxInfo.getQzblxx().add(qzblxx);
            }

        }
        return JSON.toJSONString(args);
    }

    private String getHzArgs(String ywbh, String sqh, ZggmtbForm form) {
        String sfzh = form.getSfzh();
        String xb = "1";
        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            xb = "2";
        }
        String csrq = sfzh.substring(6, 14);

        SqSavePreApplyArgs args = new SqSavePreApplyArgs();

        args.setSqlb("101"); //往来港澳通行证
        SqxxInfo sqxxInfo = new SqxxInfo();
        args.setInfo(sqxxInfo);
        TbjNdjm cjsqxx = new TbjNdjm();
        sqxxInfo.setCjsqxx(cjsqxx);
        cjsqxx.setYwbh(ywbh);
        cjsqxx.setSqh(sqh);
        cjsqxx.setYwm(form.getYwm());
        cjsqxx.setYwx(form.getYwx());
        cjsqxx.setYwxm(form.getYwx() + " " + form.getYwm());
        cjsqxx.setZwx(form.getZwx());
        cjsqxx.setZwm(form.getZwm());
        cjsqxx.setZwxm(form.getZwx() + form.getZwm());

        cjsqxx.setLxdh(form.getLxdh());
        cjsqxx.setSjhm(form.getSjhm());
        cjsqxx.setXb(xb);  //性别:男
        cjsqxx.setCsrq(csrq);
        cjsqxx.setSfzh(sfzh);
        cjsqxx.setGjdq("CHN"); //国家地区:中国
        cjsqxx.setRydylb("R"); //人员地域类型:内地居民
        cjsqxx.setSqlb("101"); //申请类别：往来港澳通行证
        cjsqxx.setCsd(form.getCsd()); //出生地:山东

        cjsqxx.setMz(form.getMz());  //民族:汉
        cjsqxx.setHkszd(form.getHkszd()); //山东省烟台市芝罘区
        cjsqxx.setJjqklxr(form.getJjqklxr());
        cjsqxx.setJjqklxrdh(form.getJjqklxrdh());

        if (form.getSqlb().contains("101")) {
            cjsqxx.setBzlb(form.getBzlbTw());  //办证类型
        } else {
            cjsqxx.setBzlb("71");   //办证类型：单独签注
        }


        //现持证件信息
        if (!"".equals(form.getXczjhmTw())) {
            cjsqxx.setXczjzl("14"); //普通护照
            cjsqxx.setXczjhm(form.getXczjhmTw());
            cjsqxx.setXczjyzqz(form.getXczjyxqzGa());
        }


        //加注信息
        if ("71".equals(form.getBzlbHz())) {
            sqxxInfo.setJzblxx(new ArrayList<TbjJzblxx>());
            int xh = 1;
            if (!"".equals(form.getXmjz())) {
                TbjJzblxx jzblxx = new TbjJzblxx();
                jzblxx.setYwbh(ywbh);
                jzblxx.setXh("" + xh);
                xh++;
                jzblxx.setZjhm(form.getXczjhmHz());
                jzblxx.setZjzl("14"); //证件种类：护照
                jzblxx.setBgjzzl("11"); //加注变更种类：护照姓名加注
                jzblxx.setBgjzxm(form.getXmjz());
                sqxxInfo.getJzblxx().add(jzblxx);
            }
            if (!"".equals(form.getCymjz())) {
                TbjJzblxx jzblxx = new TbjJzblxx();
                jzblxx.setYwbh(ywbh);
                jzblxx.setXh("" + xh);
                xh++;
                jzblxx.setZjhm(form.getXczjhmHz());
                jzblxx.setZjzl("14"); //证件种类：护照
                jzblxx.setBgjzzl("19"); //加注变更种类：护照姓名加注
                jzblxx.setBgjzxm(form.getCymjz());
                sqxxInfo.getJzblxx().add(jzblxx);
            }
            if (!"".equals(form.getCczjz())) {
                TbjJzblxx jzblxx = new TbjJzblxx();
                jzblxx.setYwbh(ywbh);
                jzblxx.setXh("" + xh);
                xh++;
                jzblxx.setZjhm(form.getXczjhmHz());
                jzblxx.setZjzl("14"); //证件种类：护照
                jzblxx.setBgjzzl("1A"); //加注变更种类：护照姓名加注
                jzblxx.setBgjzxm(form.getCczjz());
                sqxxInfo.getJzblxx().add(jzblxx);
            }

        }


        //取证信息
        cjsqxx.setSfxtkzd(form.getSfxtkzd());
        if ("1".equals(form.getSfxtkzd())) {
            cjsqxx.setEmsdz(form.getEmsdz());
            cjsqxx.setYzbm(form.getYzbm());
            cjsqxx.setSjr(form.getSjr());

            cjsqxx.setLxdh(form.getLxdh());
            cjsqxx.setSjhm(form.getSjhm());
        }


        return JSON.toJSONString(args);
    }

    private String getTwArgs(String ywbh, String sqh, ZggmtbForm form) {
        String sfzh = form.getSfzh();
        String xb = "1";
        if (Integer.parseInt(sfzh.substring(16, 17)) % 2 == 0) {
            xb = "2";
        }
        String csrq = sfzh.substring(6, 14);

        SqSavePreApplyArgs args = new SqSavePreApplyArgs();

        args.setSqlb("104"); //往来港澳通行证
        SqxxInfo sqxxInfo = new SqxxInfo();
        args.setInfo(sqxxInfo);
        TbjNdjm cjsqxx = new TbjNdjm();
        sqxxInfo.setCjsqxx(cjsqxx);
        cjsqxx.setYwbh(ywbh);
        cjsqxx.setSqh(sqh);
        cjsqxx.setYwm(form.getYwm());
        cjsqxx.setYwx(form.getYwx());
        cjsqxx.setYwxm(form.getYwx() + " " + form.getYwm());
        cjsqxx.setZwx(form.getZwx());
        cjsqxx.setZwm(form.getZwm());
        cjsqxx.setZwxm(form.getZwx() + form.getZwm());

        cjsqxx.setLxdh(form.getLxdh());
        cjsqxx.setSjhm(form.getSjhm());
        cjsqxx.setXb(xb);  //性别:男
        cjsqxx.setCsrq(csrq);
        cjsqxx.setSfzh(sfzh);
        cjsqxx.setGjdq("CHN"); //国家地区:中国
        cjsqxx.setRydylb("R"); //人员地域类型:内地居民
        cjsqxx.setSqlb("104"); //申请类别：往来港澳通行证
        if (form.getSqlb().contains("104")) {
            cjsqxx.setBzlb(form.getBzlbTw());  //办证类型
        } else {
            cjsqxx.setBzlb("81");   //办证类型：单独签注
        }


        cjsqxx.setCsd(form.getCsd()); //出生地:山东

        cjsqxx.setMz(form.getMz());  //民族:汉
        cjsqxx.setHkszd(form.getHkszd()); //山东省烟台市芝罘区
        cjsqxx.setJjqklxr(form.getJjqklxr());
        cjsqxx.setJjqklxrdh(form.getJjqklxrdh());

        //现持证件信息
        if (!"".equals(form.getXczjhmTw())) {
            cjsqxx.setXczjzl("24"); //大陆居民往来台湾通行证
            cjsqxx.setXczjhm(form.getXczjhmTw());
            cjsqxx.setXczjyzqz(form.getXczjyxqzGa());
        }


        //取证信息
        cjsqxx.setSfxtkzd(form.getSfxtkzd());
        if ("1".equals(form.getSfxtkzd())) {
            cjsqxx.setEmsdz(form.getEmsdz());
            cjsqxx.setYzbm(form.getYzbm());
            cjsqxx.setSjr(form.getSjr());

            cjsqxx.setLxdh(form.getLxdh());
            cjsqxx.setSjhm(form.getSjhm());
        }


        if (form.getQwd().contains("TWN")) {
            sqxxInfo.setQzblxx(new ArrayList<TbjQzblxx>());

            String qzcs = form.getQzcsTwn();
            String qzyxq = null;
            String qzyxqdw = null;
            String qzyxcs = qzcs.substring(3, 4);
            if (qzcs.substring(0, 2).equals("03")) {
                qzyxq = "3";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("06")) {
                qzyxq = "06";
                qzyxqdw = "月";
            } else if (qzcs.substring(0, 2).equals("12")) {
                qzyxq = "1";
                qzyxqdw = "年";
            } else if (qzcs.substring(0, 2).equals("24")) {
                qzyxq = "2";
                qzyxqdw = "年";
            } else if (qzcs.substring(0, 2).equals("36")) {
                qzyxq = "3";
                qzyxqdw = "年";
            } else if (qzcs.substring(0, 2).equals("99")) {
                qzyxq = "1";
                qzyxqdw = "年";
            }

            TbjQzblxx qzblxx = new TbjQzblxx();
            qzblxx.setYwbh(ywbh);
            qzblxx.setXh("1");
            qzblxx.setQwd("TWN"); //前往地:台湾
            qzblxx.setZjzl("24"); //证件类型：台湾通行证
            qzblxx.setQzzl(form.getQzzlTwn()); //签注类型：港澳团队旅游
            qzblxx.setQzyxq(qzyxq); //有效期
            qzblxx.setQzyxqdw(qzyxqdw); //有效期单位
            qzblxx.setQzyxcs(qzyxcs); //有效次数
            sqxxInfo.getQzblxx().add(qzblxx);
        }
        return JSON.toJSONString(args);
    }

    public int caluAge(String sfzh) {
        int birthday = Integer.parseInt(sfzh.substring(6, 14));
        int today = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));

        return (today - birthday) / 10000;
    }


}
